package com.winderinfo.yikaotianxia.ui.zhuanye;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaselanFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MajorFragment extends BaselanFragment implements Contract.View {
    @BindView(R.id.fm_web)
    WebView fmWeb;
    Unbinder unbinder;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();

    String varjs = "<script type='text/javascript'> \nwindow.onload = function()\n{var $img = document.getElementsByTagName('img');for(var p in  $img){$img[p].style.width = '100%'; $img[p].style.height ='auto'}}</script>";


    @Override
    protected int layoutResID() {
        return R.layout.fragment_major;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
        fmWeb.getSettings().setJavaScriptEnabled(true);

    }

    @Override
    protected void loadData() {
        //专业ID
        int zId = getArguments().getInt("zId");
        //请求专业详情
        /**
         * @id 专业Id
         * */
        map.clear();
        map.put("id", zId);
        presenter.onPostRequest(Api.ZHUANYEXIANGQIN, map, MajorIntroduceBean.class);

    }

    @Override
    public void onSuccess(Object data) {
        //详情数据
        if (data instanceof MajorIntroduceBean) {
            if (((MajorIntroduceBean) data).getYkAuthor() != null) {
                fmWeb.loadDataWithBaseURL("", varjs + ((MajorIntroduceBean) data).getYkAuthor().getAuthorDetails(), "text/html", "UTF-8", null);
            }
        }

    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
